package com.open.capacity.reimburse.service.impl;

import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.entity.BillReception2People;
import com.open.capacity.reimburse.entity.StdHotel;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.user.model.SysArea;
import com.open.capacity.utils.DateUtil;
import com.open.capacity.utils.StringUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.StdReception;
import com.open.capacity.reimburse.dao.StdReceptionDao;
import com.open.capacity.reimburse.service.StdReceptionService;


@Service
public class StdReceptionServiceImpl implements StdReceptionService {

    @Autowired
    private StdReceptionDao stdReceptionDao;

    /**
     * 添加
     *
     * @param stdReception
     */
    public int save(StdReception stdReception) {
        stdReception.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        stdReception.setUpdateTime(new Date());
        return stdReceptionDao.save(stdReception);
    }

    /**
     * 修改
     *
     * @param stdReception
     */
    public int update(StdReception stdReception) {
        stdReception.setUpdateTime(new Date());
        return stdReceptionDao.update(stdReception);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return stdReceptionDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public StdReception findById(String id) {
        return stdReceptionDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<StdReception> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<StdReception> list = stdReceptionDao.findAll(params);
        PageInfo<StdReception> pageInfo = new PageInfo(list);

        return PageResult.<StdReception>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<StdReception> findAll(Map<String, Object> params) {
        return stdReceptionDao.findAll(params);
    }

    /**
     * 校验接待费金额是否超标
     *
     * @param userId        当事人
     * @param followUserids 随行人员
     * @param receptionNumber    接待人
     * @param startDate     开始日期
     * @param endDate       结束日期
     * @param money         金额
     * @return
     */
    @Override
    public String verify(String userId, String followUserids, Integer receptionNumber, Date startDate, Date endDate, BigDecimal money) {
        String error_msg = "";
        int follows = (StringUtil.isEmpty(followUserids) ? 0 : followUserids.split(",").length); //陪餐人数
        int peoples = receptionNumber; //接待人数

        int days = DateUtil.getBetweenDay(startDate, endDate);
        if (days <= 0) {
            days = 1;
        }
        Map<String, Object> params = new HashMap<>();
        List<StdReception> list = stdReceptionDao.findAll(params);

        boolean flag;
        if (CollectionUtils.isNotEmpty(list)) {
            StdReception item = list.get(0);

            if ((peoples <= item.getPersonMin() && follows > item.getAccompanyLimit()) || (peoples > item.getPersonMin() && follows > item.getAccompanyLimit() && ((float) follows / peoples) > (item.getPercent() / 100))) {
                error_msg = "<p>⊙ 陪餐人数超标。</p>";
                return error_msg;
            }

            BigDecimal standard = item.getMoney().multiply(BigDecimal.valueOf(days)).multiply(BigDecimal.valueOf(follows + peoples));
            flag = money.compareTo(standard) <= 0;
            if (!flag) {
                DecimalFormat decimalFormat = new DecimalFormat("###,###.00");
                error_msg = "<p>⊙ 接待费用超标, 接待费用合计不得超过" + decimalFormat.format(standard) + "元</p>";
            }
        }
        return error_msg;
    }
}
